<template><div><h3 id="_1-变量" tabindex="-1"><a class="header-anchor" href="#_1-变量" aria-hidden="true">#</a> 1 变量</h3>
<h4 id="_1-1-系统预设变量" tabindex="-1"><a class="header-anchor" href="#_1-1-系统预设变量" aria-hidden="true">#</a> 1.1 系统预设变量</h4>
<p><strong>常用系统变量</strong>：$HOME、$PWD、$SHELL、$USER</p>
<p><strong>查看当前shell中的所有变量</strong>：<code v-pre>set</code></p>
<h4 id="_1-2-自定义变量" tabindex="-1"><a class="header-anchor" href="#_1-2-自定义变量" aria-hidden="true">#</a> 1.2 自定义变量</h4>
<ul>
<li>定义变量：<code v-pre>变量名=变量值</code>，”=“前后不能有空格</li>
<li>撤销变量：<code v-pre>unset 变量名</code></li>
<li>声明只读变量：<code v-pre>readonly 变量</code>，无法撤销</li>
<li>变量作用域转换为全局环境变量：<code v-pre>export 变量名</code></li>
</ul>
<div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li>变量名由字母、数字、下划线组成，不能以数字开头</li>
<li>变量默认为字符串类型，无法进行数值运算</li>
<li>变量值包含空格时，应用单引号或双引号包起来</li>
<li>全局环境变量可以被其他文件使用</li>
</ul>
</div>
<h4 id="_1-3-特殊变量" tabindex="-1"><a class="header-anchor" href="#_1-3-特殊变量" aria-hidden="true">#</a> 1.3 特殊变量</h4>
<h5 id="_1-n" tabindex="-1"><a class="header-anchor" href="#_1-n" aria-hidden="true">#</a> (1) $n</h5>
<ul>
<li><code v-pre>$0</code>表示脚本的名称</li>
<li><code v-pre>$1~$9</code>表示第一到第九个参数</li>
<li>第十个及以上的参数需要用大括号包着，如<code v-pre>${10}</code></li>
</ul>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code>./parameter.sh a b c
<span class="token variable">$0</span><span class="token operator">=</span>parameter.sh
<span class="token variable">$1</span><span class="token operator">=</span>a
<span class="token variable">$2</span><span class="token operator">=</span>b
<span class="token variable">$3</span><span class="token operator">=</span>c
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h5 id="_2" tabindex="-1"><a class="header-anchor" href="#_2" aria-hidden="true">#</a> (2) $#</h5>
<ul>
<li>获取参数个数，不包含<code v-pre>$0</code></li>
</ul>
<h5 id="_3-、" tabindex="-1"><a class="header-anchor" href="#_3-、" aria-hidden="true">#</a> (3) $*、$@</h5>
<ul>
<li>相同点：二者都用来获取参数列表</li>
<li>不同点：参数列表用引号包着时，<code v-pre>$*</code>会将引号内的内容按照空格拆分，当成多个参数；<code v-pre>$@</code>则将其当成一个参数。</li>
</ul>
<h5 id="_4" tabindex="-1"><a class="header-anchor" href="#_4" aria-hidden="true">#</a> (4) $?</h5>
<ul>
<li>返回最后一次命令的执行状态。返回0，证明上一个命令正确执行；返回非0，则未正确执行。</li>
</ul>
<h4 id="_1-4-变量子串" tabindex="-1"><a class="header-anchor" href="#_1-4-变量子串" aria-hidden="true">#</a> 1.4 变量子串</h4>
<ul>
<li><code v-pre>${parameter}</code> 返回变量内容</li>
<li><code v-pre>${#parameter}</code> 返回变量内容长度</li>
<li><code v-pre>${parameter:offset}</code> 提取从offset到结尾的子串</li>
<li><code v-pre>${parameter:offset:length}</code> 提取从offset开始，长度为length的子串</li>
<li><code v-pre>${parameter#word}</code> 从变量开头删除最短匹配word的子串</li>
<li><code v-pre>${parameter##word}</code> 从变量开头删除最长匹配word的子串</li>
<li><code v-pre>${parameter%word}</code> 从变量结尾删除最短匹配word的子串</li>
<li><code v-pre>${parameter%%word}</code> 从变量结尾删除最长匹配word的子串</li>
<li><code v-pre>${parameter/pattern/string}</code> 使用string代替第一个匹配的pattern</li>
<li><code v-pre>${parameter//pattern/string}</code> 使用string代替所有匹配的pattern</li>
</ul>
<h4 id="_1-5-扩展变量" tabindex="-1"><a class="header-anchor" href="#_1-5-扩展变量" aria-hidden="true">#</a> 1.5 扩展变量</h4>
<ul>
<li><code v-pre>${parameter:-word}</code> 如果parameter为空或未赋值时，返回word替代parameter。</li>
<li><code v-pre>${parameter:=word}</code> 如果parameter为空或未赋值时，设置这个变量为word，并返回。</li>
<li><code v-pre>${parameter:?word}</code> 如果parameter为空或未赋值时，将word作为标准错误输出。</li>
<li><code v-pre>${parameter:+word}</code> 如果parameter为空或未赋值，则什么都不做，否则使用word替换parameter的值。</li>
</ul>
<h3 id="_2-运算符" tabindex="-1"><a class="header-anchor" href="#_2-运算符" aria-hidden="true">#</a> 2 运算符</h3>
<ul>
<li><code v-pre>$[运算式]</code>或者<code v-pre>$((运算式))</code></li>
</ul>
<h3 id="_3-条件判断" tabindex="-1"><a class="header-anchor" href="#_3-条件判断" aria-hidden="true">#</a> 3 条件判断</h3>
<p><strong>语法</strong></p>
<ul>
<li><code v-pre>test condition</code></li>
<li><code v-pre>[ condition ]</code>，注意condition前后都要有空格，条件非空返回true，空则返回false。</li>
<li><code v-pre>[[ condition ]]</code>，在<code v-pre>[[]]</code> 中可以使用通配符进行模式匹配；并且<code v-pre>&amp;&amp;、||、&gt;、&lt;</code> 可以使用。</li>
<li><code v-pre>((表达式))</code> 常用于计算 ，如整数运算</li>
</ul>
<div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li><code v-pre>test condition</code> 等价于 <code v-pre>[ condition ]</code>。</li>
<li><code v-pre>&amp;&amp;、||、&gt;、&lt;</code> 等操作符可以用在<code v-pre>[[]]</code> 中，不能用在<code v-pre>[]</code> 中。<code v-pre>[]</code> 使用<code v-pre>-a</code>、<code v-pre>-o</code>、<code v-pre>-gt</code>(用于整数)、<code v-pre>-lt</code>(用于整数) 代替以上运算符。</li>
<li><code v-pre>(())</code> 用于算术运算</li>
</ul>
</div>
<p><strong>文件判断</strong></p>
<ul>
<li><code v-pre>-d</code> 判断文件存在且类型为目录。</li>
<li><code v-pre>-f</code> 判断文件存在且类型为文件。</li>
<li><code v-pre>-e</code> 判断文件存在。</li>
<li><code v-pre>-r</code> 判断文件存在且可读。</li>
<li><code v-pre>-s</code> 判断文件存在且大小不为0。</li>
<li><code v-pre>-w</code> 判断文件存在且可写。</li>
<li><code v-pre>-x</code> 判断文件存在且可执行。</li>
<li><code v-pre>-L</code> 判断文件存在且为链接文件。</li>
<li><code v-pre>f1 -nt f2</code> 判断文件f1比文件f2新，按照文件修改时间计算。</li>
<li><code v-pre>f1 -ot f2</code> 判断文件f1比文件f2旧，按照文件修改时间计算。</li>
</ul>
<p><strong>特殊条件表达式</strong></p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token punctuation">[</span> condition <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span><span class="token punctuation">{</span>
	code
<span class="token punctuation">}</span>

<span class="token punctuation">[</span><span class="token punctuation">[</span> condition <span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span><span class="token punctuation">{</span>
	code
<span class="token punctuation">}</span>

<span class="token builtin class-name">test</span> condition <span class="token operator">&amp;&amp;</span><span class="token punctuation">{</span>
	code
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>等同于</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">if</span> <span class="token punctuation">[</span> condition <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
	code
<span class="token keyword">fi</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>字符串判断</strong></p>
<ul>
<li><code v-pre>-n</code> 字符串长度不为0，则为真。</li>
<li><code v-pre>-z</code> 字符串长度为0，则为真。</li>
<li><code v-pre>&quot;串1&quot; = &quot;串2&quot;</code> 两串相等，则为真。</li>
<li><code v-pre>&quot;串1&quot; != &quot;串2&quot;</code> 两串不等，则为真。</li>
</ul>
<div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li>字符串判断，一定要将字符串加双引号再比较。</li>
<li><code v-pre>=</code>、<code v-pre>!=</code> 两端一定有空格。</li>
</ul>
</div>
<p><strong>整数比较</strong></p>
<table>
<thead>
<tr>
<th style="text-align:center"><code v-pre>[]</code> 和test</th>
<th style="text-align:center"><code v-pre>(())</code>和<code v-pre>[[]]</code></th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">-eq</td>
<td style="text-align:center">==或=</td>
<td style="text-align:center">相等</td>
</tr>
<tr>
<td style="text-align:center">-ne</td>
<td style="text-align:center">!=</td>
<td style="text-align:center">不想等</td>
</tr>
<tr>
<td style="text-align:center">-gt</td>
<td style="text-align:center">&gt;</td>
<td style="text-align:center">大于</td>
</tr>
<tr>
<td style="text-align:center">-ge</td>
<td style="text-align:center">&gt;=</td>
<td style="text-align:center">大于等于</td>
</tr>
<tr>
<td style="text-align:center">-lt</td>
<td style="text-align:center">&lt;</td>
<td style="text-align:center">小于</td>
</tr>
<tr>
<td style="text-align:center">-le</td>
<td style="text-align:center">&lt;=</td>
<td style="text-align:center">小于等于</td>
</tr>
</tbody>
</table>
<div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li><code v-pre>=</code> 和 <code v-pre>!=</code> 也可在<code v-pre>[]</code> 中使用，但在<code v-pre>[]</code>使用<code v-pre>&gt;</code>、<code v-pre>&lt;</code>时，需要用反斜杠转义，否则可能计算出错。</li>
<li>比较符号两侧要有空格。</li>
</ul>
</div>
<p><strong>逻辑运算符</strong></p>
<table>
<thead>
<tr>
<th style="text-align:center"><code v-pre>[]</code>和test</th>
<th style="text-align:center"><code v-pre>(())</code>和<code v-pre>[[]]</code></th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">-a</td>
<td style="text-align:center">&amp;&amp;</td>
<td style="text-align:center">and</td>
</tr>
<tr>
<td style="text-align:center">-o</td>
<td style="text-align:center">||</td>
<td style="text-align:center">or</td>
</tr>
<tr>
<td style="text-align:center">!</td>
<td style="text-align:center">!</td>
<td style="text-align:center">not</td>
</tr>
</tbody>
</table>
<div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li><code v-pre>&amp;&amp;</code>和<code v-pre>||</code>逻辑操作符号可用于<code v-pre>[[]]</code>或<code v-pre>(())</code> 中，也可在外部连接多个<code v-pre>[]</code>。</li>
</ul>
</div>
<p><strong>总结</strong></p>
<table>
<thead>
<tr>
<th style="text-align:center">表达式</th>
<th style="text-align:center"><code v-pre>[]</code></th>
<th style="text-align:center"><code v-pre>test</code></th>
<th style="text-align:center"><code v-pre>[[]]</code></th>
<th style="text-align:center"><code v-pre>(())</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">边界是否需要空格</td>
<td style="text-align:center">需要</td>
<td style="text-align:center">需要</td>
<td style="text-align:center">需要</td>
<td style="text-align:center">不需要</td>
</tr>
<tr>
<td style="text-align:center">逻辑操作符</td>
<td style="text-align:center">!、-a、-o</td>
<td style="text-align:center">!、-a、-o</td>
<td style="text-align:center">!、&amp;&amp;、||</td>
<td style="text-align:center">!、&amp;&amp;、||</td>
</tr>
<tr>
<td style="text-align:center">整数比较符</td>
<td style="text-align:center">-eq、-gt、-lt、-ge、-le</td>
<td style="text-align:center">-eq、-gt、-lt、-ge、-le</td>
<td style="text-align:center">-eq、-gt、-lt、-ge、-le 或者 =、&gt;、&lt;、&gt;=、&lt;=</td>
<td style="text-align:center">=、&gt;、&lt;、&gt;=、&lt;=</td>
</tr>
<tr>
<td style="text-align:center">字符串比较符</td>
<td style="text-align:center">=、==、!=</td>
<td style="text-align:center">=、==、!=</td>
<td style="text-align:center">=、==、!=</td>
<td style="text-align:center">=、==、!=</td>
</tr>
<tr>
<td style="text-align:center">是否支持通配符</td>
<td style="text-align:center">不支持</td>
<td style="text-align:center">不支持</td>
<td style="text-align:center">支持</td>
<td style="text-align:center">不支持</td>
</tr>
</tbody>
</table>
<h3 id="_4-流程控制" tabindex="-1"><a class="header-anchor" href="#_4-流程控制" aria-hidden="true">#</a> 4 流程控制</h3>
<h4 id="_4-1-if" tabindex="-1"><a class="header-anchor" href="#_4-1-if" aria-hidden="true">#</a> 4.1 if</h4>
<p>(1) 单分支</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">if</span> <span class="token punctuation">[</span> condition <span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">then</span>
	code
<span class="token keyword">fi</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">if</span> <span class="token punctuation">[</span> condition <span class="token punctuation">]</span>
<span class="token keyword">then</span>
	code
<span class="token keyword">fi</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>(2) 多分支</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">if</span> <span class="token punctuation">[</span> condition1 <span class="token punctuation">]</span>
<span class="token keyword">then</span>
	code
<span class="token keyword">elif</span> <span class="token punctuation">[</span> condition2 <span class="token punctuation">]</span>
<span class="token keyword">then</span>
	code
<span class="token keyword">else</span>
	code
<span class="token keyword">fi</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li><code v-pre>if</code> 后要有空格</li>
</ul>
</div>
<h4 id="_4-2-case" tabindex="-1"><a class="header-anchor" href="#_4-2-case" aria-hidden="true">#</a> 4.2 case</h4>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">case</span> <span class="token variable">$var</span> <span class="token keyword">in</span>
<span class="token string">"value1"</span><span class="token punctuation">)</span>
	code
<span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token string">"value2"</span><span class="token punctuation">)</span>
	code
<span class="token punctuation">;</span><span class="token punctuation">;</span>
*<span class="token punctuation">)</span>
	code
<span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token keyword">esac</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li>每一个匹配项必须以<code v-pre>)</code>结束。</li>
<li>每个匹配代码块，结束要用<code v-pre>;;</code>，相当于<code v-pre>break</code>。</li>
<li><code v-pre>*)</code> 相当于<code v-pre>default</code>。</li>
</ul>
</div>
<h4 id="_4-3-for" tabindex="-1"><a class="header-anchor" href="#_4-3-for" aria-hidden="true">#</a> 4.3 for</h4>
<p>(1) 方式一</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">for</span> <span class="token variable"><span class="token punctuation">((</span> 初始值<span class="token punctuation">;</span>循环条件<span class="token punctuation">;</span>变量变化 <span class="token punctuation">))</span></span>
<span class="token keyword">do</span>
	code
<span class="token keyword">done</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li>数学符号判断，<code v-pre>&lt;</code>、<code v-pre>=</code>、<code v-pre>&gt;</code></li>
<li><code v-pre>(())</code> 双括号</li>
</ul>
</div>
<p>(2) 方式二</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">for</span> <span class="token for-or-select variable">var</span> <span class="token keyword">in</span> value1 value2 value3
<span class="token keyword">do</span> 
	code
<span class="token keyword">done</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_4-4-while" tabindex="-1"><a class="header-anchor" href="#_4-4-while" aria-hidden="true">#</a> 4.4 while</h4>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">while</span> <span class="token punctuation">[</span> condition <span class="token punctuation">]</span>
<span class="token keyword">do</span>
	code
<span class="token keyword">done</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="_5-读取控制台输入" tabindex="-1"><a class="header-anchor" href="#_5-读取控制台输入" aria-hidden="true">#</a> 5 读取控制台输入</h3>
<p><strong>语法</strong></p>
<p><code v-pre>read [选项] [参数]</code></p>
<p><strong>选项</strong></p>
<ul>
<li><code v-pre>-p</code> 指定读取时的提示符。</li>
<li><code v-pre>-t</code> 指定读取时等待时间，不加则一直等。</li>
</ul>
<p><strong>参数</strong></p>
<ul>
<li>值存储的变量名</li>
</ul>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token builtin class-name">read</span> -t <span class="token number">7</span> -p <span class="token string">"Enter your name in 7 seconds :"</span> name
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h3 id="_6-函数" tabindex="-1"><a class="header-anchor" href="#_6-函数" aria-hidden="true">#</a> 6 函数</h3>
<h4 id="_6-1-系统函数" tabindex="-1"><a class="header-anchor" href="#_6-1-系统函数" aria-hidden="true">#</a> 6.1 系统函数</h4>
<p>(1) <code v-pre>basename</code>：获取pathname中的文件</p>
<p><strong>语法</strong></p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token function">basename</span> <span class="token punctuation">[</span>pathname<span class="token punctuation">]</span> <span class="token punctuation">[</span>suffix<span class="token punctuation">]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><ul>
<li><code v-pre>pathname</code> 路径名称</li>
<li><code v-pre>suffix</code> 后缀名</li>
</ul>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token function">basename</span> /home/abc.txt
<span class="token comment"># abc.txt</span>
<span class="token function">basename</span> /home/abc.txt .txt
<span class="token comment"># abc</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>(2) <code v-pre>dirname</code>: 获取文件的路径</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token function">dirname</span> /home/abc.txt
<span class="token comment"># /home</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_6-2-自定义函数" tabindex="-1"><a class="header-anchor" href="#_6-2-自定义函数" aria-hidden="true">#</a> 6.2 自定义函数</h4>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">function</span> <span class="token function-name function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	code
	<span class="token builtin class-name">return</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li>函数使用之前必须先声明。</li>
<li>函数返回值，未显示加return时，可以通过<code v-pre>$?</code>获得；不加return默认返回最后一条命令运行结果。</li>
</ul>
</div>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">function</span> <span class="token function-name function">sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token assign-left variable">s</span><span class="token operator">=</span><span class="token number">0</span>
	<span class="token assign-left variable">s</span><span class="token operator">=</span>$<span class="token punctuation">[</span><span class="token variable">$1</span>+<span class="token variable">$2</span><span class="token punctuation">]</span>
	<span class="token builtin class-name">echo</span> <span class="token string">'$s'</span>
<span class="token punctuation">}</span>
<span class="token assign-left variable">num1</span><span class="token operator">=</span><span class="token number">1</span>
<span class="token assign-left variable">num2</span><span class="token operator">=</span><span class="token number">2</span>
<span class="token function">sum</span> <span class="token variable">$num1</span> <span class="token variable">$num2</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="_7-文本处理工具" tabindex="-1"><a class="header-anchor" href="#_7-文本处理工具" aria-hidden="true">#</a> 7 文本处理工具</h3>
<h4 id="_7-1-cut" tabindex="-1"><a class="header-anchor" href="#_7-1-cut" aria-hidden="true">#</a> 7.1 cut</h4>
<p><strong>语法</strong></p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token function">cut</span> <span class="token punctuation">[</span>参数<span class="token punctuation">]</span> filename
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><ul>
<li><code v-pre>-f</code> 列号，取第几列</li>
<li><code v-pre>-d</code> 分隔符，按照指定分割符分割列，默认为制表符。</li>
<li><code v-pre>-c</code> 按字符进行切割，后加n取第n个字符，可加范围，如：<code v-pre>5-7</code></li>
</ul>
<h4 id="_7-2-awk" tabindex="-1"><a class="header-anchor" href="#_7-2-awk" aria-hidden="true">#</a> 7.2 awk</h4>
<p><strong>语法</strong></p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token function">awk</span> <span class="token punctuation">[</span>参数<span class="token punctuation">]</span> <span class="token string">'/pattern1/{action1}'</span> <span class="token string">'/pattern2/{action2}'</span> filename
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><ul>
<li><code v-pre>pattern</code> 正则表达式</li>
<li><code v-pre>action</code> 对匹配到的内容执行对应操作</li>
</ul>
<p><strong>参数</strong></p>
<ul>
<li><code v-pre>-F</code> 输入文件分隔符</li>
<li><code v-pre>-v</code> 赋值一个用户定义变量</li>
</ul>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token comment"># 数据格式：用户名:密码:用户id:组id:注释:用户家目录:shell解析器 文件名 passwd</span>
<span class="token function">awk</span> -F <span class="token builtin class-name">:</span> <span class="token string">'/^root/{print $1","$7}'</span> <span class="token function">passwd</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>内置变量</strong></p>
<ul>
<li><code v-pre>FILENAME</code> 文件名</li>
<li><code v-pre>NR</code> 已读记录数</li>
<li><code v-pre>NF</code> 分割后，列的个数</li>
</ul>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token function">awk</span> -F <span class="token builtin class-name">:</span> <span class="token string">'{print "filename:" FILENAME ",linenum:" NR ",col:" NF}'</span> <span class="token function">passwd</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p><strong>文件头和文件尾</strong></p>
<ul>
<li><code v-pre>BEGIN</code> 文件头加内容</li>
<li><code v-pre>END</code> 文件尾加内容</li>
</ul>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token function">awk</span> -F <span class="token builtin class-name">:</span> <span class="token string">'BEGIN{print "user, shell"}{print $1","$7}END{print "dahaige,/bin/zuishuai"}'</span> <span class="token function">passwd</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div></div></template>
